<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/2/4
 * Time: 16:10
 */

namespace app\index\controller;


use app\admin\model\Category;
use app\common\controller\HomeBaseController;

class Lists extends HomeBaseController
{
    //首页
    public function index() {
        $headConf = ['title' => 'ZeBlog'];
        $this->assign('headConf', $headConf);
        //获取左侧第一部分数据
        $cate_id = input('param.cate_id');
        $tag_id = input('param.tid');
        if ($cate_id) {
            //当前分类所有子集分类id
//            halt(db('category')->select());
            $cids = (new Category())->getSon(db('category')->select(), $cate_id);
            $cids[] = $cate_id;//把自己追加进去
            $headData = [
                'title' => '分类',
                'name' => db('category')->where('cate_id', $cate_id)->value('cate_name'),
                'total' => db('article')->whereIn('cate_id', $cids)->count(),
            ];
            //获取文章数据
            $articleData = db('article')->alias('a')
                ->join('__CATEGORY__ c', 'a.cate_id=c.cate_id')
                ->where('a.is_recycle', 2)
                ->whereIn('a.cate_id', $cids)
                ->paginate(PAGINAGE_ARTICLE['webset_value'])
                ->each(function ($item, $key){
                    $item['tags'] = db('arc_tag')->alias('at')
                        ->join('__TAG__ t', 'at.tid=t.tid')
                        ->where('at.arc_id', $item['arc_id'])
                        ->field('t.tid,t.tname')
                        ->select();
                    return $item;
                });
        }
        if ($tag_id) {
            $headData = [
                'title' => '标签',
                'name' => db('tag')->where('tid', $tag_id)->value('tname'),
                'total' => db('arc_tag')->where('tid', $tag_id)->count(),
            ];
            //获取文章数据
            $articleData = db('article')->alias('a')
                ->join('__ARC_TAG__ at', 'a.arc_id=at.arc_id')
                ->join('__CATEGORY__ c', 'a.cate_id=c.cate_id')
                ->where('a.is_recycle', 2)->where("at.tid", $tag_id)
                ->paginate(PAGINAGE_ARTICLE['webset_value'])
                ->each(function ($item, $key){
                    $item['tags'] = db('arc_tag')->alias('at')
                        ->join('__TAG__ t', 'at.tid=t.tid')
                        ->where('at.arc_id', $item['arc_id'])
                        ->field('t.tid,t.tname')
                        ->select();
                    return $item;
                });
        }

//        foreach ($articleData as $k => $v) {
//            $articleData[$k]['tags'] = db('arc_tag')->alias('at')
//                ->join('__TAG__ t', 'at.tid=t.tid')
//                ->where('at.arc_id', $v['arc_id'])->field('t.tid,t.tname')->select();
//        }
//        halt($articleData);
        $page = $articleData->render();
        $this->assign('page',$page);
        $this->assign('headData', $headData);
        $this->assign('articleData', $articleData);
        return $this->fetch();
    }


}